﻿<?php
$begintime = microtime(true);
require_once './lib.php';
$mysql = new DBAccess();
header("Content-type:text/html;charset=utf-8");
/*ignore_user_abort();
set_time_limit(0);

do {*/
$typeId = array(
     array('type' => 18, 'code' => '/pks/getLotteryPksInfo.do?lotCode=10001'), //北京PK拾
    array('type' => 1, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10002'), //重庆时时彩
    array('type' => 25, 'code' => '/lotteryJSFastThree/getBaseJSFastThree.do?lotCode=10007'), //江苏快三
    array('type' => 6, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10006'), //广东11选5
    array('type' => 26, 'code' => '/lotteryJSFastThree/getBaseJSFastThree.do?lotCode=10026'), //好运快三  即  广西快3
    array('type' => 16, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10015'), //江西多乐彩  即  江西11选5
    array('type' => 7, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10019'), //辽宁11选5
    array('type' => 8, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10018'), //上海11选5
    array('type' => 15, 'code' => 10012), //重庆11选5
    array('type' => 12, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10004'), //新疆时时彩
    array('type' => 10, 'code' => '/QuanGuoCai/getLotteryInfo1.do?&lotCode=10043'), //排列3
    array('type' => 9, 'code' => '/QuanGuoCai/getLotteryInfo1.do?&lotCode=10041'), //福彩3D
    array('type' => 36, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10003') //天津时时彩
);
/*$typeId2 = array(
 array('type'=>3 ,'code'=>????),  //江西时时彩
 array('type'=>2 ,'code'=>10067)   //幸运11选5	即  黑龙江11选5
 );*/


foreach ($typeId as $v) {
    $curl = curl_init();
//    	curl_setopt($curl, CURLOPT_URL, 'http://123.168879.com/Open/CurrentOpenOne?code=' . $v['code']);
    curl_setopt($curl, CURLOPT_URL, 'api.api68.com' . $v['code']);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    $data = curl_exec($curl);
    $data = json_decode(trim($data), true);
    curl_close($curl);
    $data = $data['result']['data'];
    $c_t = trim($data['preDrawIssue']); //开奖期数
    $c_r = trim($data['preDrawCode']); //开奖号码
    if ($c_t && $c_r) {
        $type = $v['type'];
        //判断彩票类型，根据彩票类型将数据存到数据库中并做相应验证和结算。
        if ($type == 1) {
            $sql = "select id from lottery_result_cq WHERE qishu='$data[preDrawIssue]'";
            $res = $mysql->getRow($sql);//查询数据库中是否存在最新期的数据，如果不存在则将数据插入到数据库中
            if (!$res) {
                $ins_res = array(
                    'qishu' => $data['preDrawIssue'],
                    'create_time' => $data['serverTime'],
                    'datetime' => date('Y-m-d H:i:s'),
                    'state' => '0',
                    "ball_1" => $data['firstNum'],
                    "ball_2" => $data['secondNum'],
                    "ball_3" => $data['thirdNum'],
                    "ball_4" => $data['fourthNum'],
                    "ball_5" => $data['fifthNum'],
                );
                $mysql->insertRow('lottery_result_cq', $ins_res);
                /*将抓取到的数据添加到数据库中，因为表为分离式所以判断繁琐*/
            }
            //对当前开奖号码转换开奖规则
            $niuniu = b5_niuniu($data['firstNum'], $data['secondNum'], $data['thirdNum'], $data['fourthNum'], $data['fifthNum']);
            $hm[] = $data['firstNum'];//第一个球
            $hm[] = $data['secondNum'];//第二个球
            $hm[] = $data['thirdNum'];//第三个球
            $hm[] = $data['fourthNum'];//第四个球
            $hm[] = $data['fifthNum'];//第五个球
            /*测试数据*/
//            $hm[] = 2;
//            $hm[] = 1;
//            $hm[] = 5;
//            $hm[] = 2;
//            $hm[] = 5;
            $hms['zhs'][] = Ssc_Auto($hm, 1);//算总和
            $hms['zh'][] = Ssc_Auto($hm, 2);//算总和大小
            $hms['zh'][] = Ssc_Auto($hm, 3);//算总和单双
            $hms['zh'][] = Ssc_Auto($hm, 4);//算龙虎和
            $hms['three'][] = Ssc_Auto($hm, 5);//算前三
            $hms['three'][] = Ssc_Auto($hm, 6);//算中三
            $hms['three'][] = Ssc_Auto($hm, 7);//算后三
            $hms['niuniu'][] = $niuniu;//牛牛
            $hms['niuniu'][] = b5_niuds($niuniu);//牛大
            $hms['niuniu'][] = b5_niudx($niuniu);//牛小
//            var_dump($hms);
//            var_dump($hm);

//            $hmlist[$c_t][] = $data['firstNum'] . ',' . $data['secondNum'] . ',' . $data['thirdNum'] . ',' . $data['fourthNum'] . ',' . $data['fifthNum'];
//            var_dump($arr);

//            $sql="update order_lottery_sub set status=0,is_win=0";
//            $sql1="update order_lottery set status=0";
//            $mysql->query($sql);
//            $mysql->query($sql1);
//            exit();
            $sql = "select * from order_lottery_sub WHERE order_num=ANY (select order_lottery.order_num from order_lottery WHERE lottery_number= $data[preDrawIssue]) AND status=0";
            //$sql = "select * from order_lottery_sub WHERE order_num=ANY(select order_lottery.order_num from order_lottery WHERE lottery_number= 20171228082) AND status=0";
            $res = $mysql->getRows($sql);
            $num = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
            $dx = array('大', '小');
            $ds = array('单', '双');
            $win = 0;
//            var_dump($hms);
//            var_dump($res);
//            exit();
            $zjresultall = array();
            foreach ($res as $k => $v) {
                if ($v['quick_type'] == "第一球") {
                    if (in_array($v['number'], $num)) {
                        if ($v['number'] == $hm[0]) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $dx)) {
                        if ($v['number'] == Ssc_Dx($ins_res['ball_1'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $ds)) {
                        if ($v['number'] == Ssc_Ds($ins_res['ball_1'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                }
                if ($v['quick_type'] == "第二球") {
                    if (in_array($v['number'], $num)) {
                        if ($v['number'] == $ins_res['ball_2']) {

                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $dx)) {
                        if ($v['number'] == Ssc_Dx($ins_res['ball_2'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $ds)) {
                        if ($v['number'] == Ssc_Ds($ins_res['ball_2'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                }
                if ($v['quick_type'] == "第三球") {
                    if (in_array($v['number'], $num)) {
                        if ($v['number'] == $ins_res['ball_3']) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $dx)) {
                        if ($v['number'] == Ssc_Dx($ins_res['ball_3'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $ds)) {
                        if ($v['number'] == Ssc_Ds($ins_res['ball_3'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                }
                if ($v['quick_type'] == "第四球") {
                    if (in_array($v['number'], $num)) {
                        if ($v['number'] == $ins_res['ball_4']) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $dx)) {
                        if ($v['number'] == Ssc_Dx($ins_res['ball_4'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $ds)) {
                        if ($v['number'] == Ssc_Ds($ins_res['ball_4'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }

                    }
                }
                if ($v['quick_type'] == "第五球") {
                    if (in_array($v['number'], $num)) {
                        if ($v['number'] == $ins_res['ball_5']) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $dx)) {
                        if ($v['number'] == Ssc_Dx($ins_res['ball_5'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                    if (in_array($v['number'], $ds)) {
                        if ($v['number'] == Ssc_Ds($ins_res['ball_5'])) {
                            $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                            array_push($zjresultall, $array);
                        }
                    }
                }
                if ($v['quick_type'] == "总和龙虎和") {
                    if (in_array($v['number'], $hms['zh'])) {
                        $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                        array_push($zjresultall, $array);
                    }
                }
                if ($v['quick_type'] == "前三") {
                    if (in_array($v['number'], $hms['three'])) {
                        $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                        array_push($zjresultall, $array);
                    }
                }
                if ($v['quick_type'] == "中三") {
                    if (in_array($v['number'], $hms['three'])) {
                        $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                        array_push($zjresultall, $array);
                    }
                }
                if ($v['quick_type'] == "后三") {
                    if (in_array($v['number'], $hms['three'])) {
                        $array = array("win" => $v['win'], "order_sub_num" => $v['order_sub_num'], "order_num" => $v['order_num']);
                        array_push($zjresultall, $array);
                    }
                }
            }
        }
    }
}

/**
 * 如果存在中奖信息则通过foreach循环将数据存到数据库中
 * 如果不存在中奖信息则表示没有中奖信息，直接将状态和中奖情况通过查询订单号更新数据库信息
 */

if ($zjresultall) {
    foreach ($zjresultall as $k => $v) {

        $win = 0.0;
        //更新子表信息
        // $sql = "update order_lottery_sub set ('is_win'=1,'status'=1) WHERE order_num=$k AND order_sub_num='$v[order_sub_num]'";
        $set = array('is_win' => 1, 'status' => 1);
        $mysql->updateRows_me('order_lottery_sub', $set, "order_num='$v[order_num]' AND order_sub_num='$v[order_sub_num]'");
        $win = (float)$v['win'];
        //更新用户中奖金额，并加入用户余额中
        $sql_money = "update user_list set money=money+$win WHERE user_id=(SELECT user_id FROM order_lottery WHERE order_num=$v[order_num])";
        $mysql->query($sql_money);
        //$sql = "update order_lottery_sub set ('is_win'=0,'status'=1) WHERE order_num='$v[order_num] And status=0'";
        $set = array('is_win' => 0, 'status' => 1);
        $mysql->updateRows_me('order_lottery_sub', $set, "order_num=$v[order_num] AND status=0");
        //更新主表信息
        $sql = "update order_lottery set status='1' WHERE order_num=$v[order_num] and status=0";
        $mysql->query($sql);
    }
}
/*
 * 如果数据为空则表示没有中奖，直接修改子订单状态和订单中奖情况，并更新主表
 */
if (empty($zjresultall)) {
    //更新子表信息
    //UPDATE order_lottery_sub set `status`=1,is_win=0 WHERE order_num=ANY(select order_num from order_lottery WHERE lottery_number=20171228082);
    $sql = "UPDATE order_lottery_sub set `status`=1,is_win=0 WHERE order_num=ANY(select order_num from order_lottery WHERE lottery_number=$c_t) WHERE status=0";
    $mysql->query($sql);
    //更新主表信息
    $sql = "UPDATE order_lottery set `status`=1 WHERE lottery_number=$c_t";
    $mysql->query($sql);
}

//if ($win) {
//    $sql = "update order_lottery set status='1' WHERE order_num=$order_num and status=0";
//    $sql_money = "update user_list set money=money+$win WHERE user_id=(SELECT user_id FROM order_lottery WHERE order_num=$order_num)";
//    echo $sql_money . "<br>";
//    $mysql->update($sql);
//    $mysql->update($sql_money);
//
//}
$endtime = microtime(true);
$resultime = $endtime - $begintime;
echo "页面运行时间：$resultime";
